﻿using JNPF.Basics.Logic;
using JNPF.Basics.Models;
using JNPF.Basics.Models.WeChat.Dtos.MPUser;
using JNPF.Utils;
using JNPF.Utils.Controller;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;

namespace JNPF.Basics.Controller
{
    /// <summary>
    /// 公众号用户
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司（https://www.jnpfsoft.com）
    /// 作 者：JNPF开发平台组
    /// 日 期：2018-09-12 
    /// </summary>
    [Route("api/WeChat/MPUser")]
    public class MPUserController : BaseApiController
    {
        private MPUserBll mPUserBll = new MPUserBll();

        #region Get
        /// <summary>
        /// 获取标签用户列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <param name="type">用户：user-已关注、unblack-黑名单、标签用户列表</param>
        /// <returns></returns>
        [HttpGet("{type}")]
        public async Task<ResponseResult> GetTagUsersList([FromQuery] PageInput input, string type)
        {
            try
            {
                var data = new PageResult<MPUserModel>();
                if (type == "user")
                    data = await mPUserBll.GetList(input);
                else if (type == "unblack")
                    data = await mPUserBll.GetBlackList(input);
                else
                    data = await mPUserBll.GetListByTagId(input, type);
                if (!input.keyword.IsEmpty())
                {
                    data.list = data.list.FindAll(x => x.nickname.Contains(input.keyword) || x.country.Contains(input.keyword) || x.city.Contains(input.keyword) || x.remark.Contains(input.keyword) || x.province.Contains(input.keyword));
                    data.pagination.total = data.list.Count;
                }
                var output = new PageResult()
                {
                    list = data.list.MapToList<MPUserListOutput>(),
                    pagination = data.pagination

                };
                return Success(output);
            }
            catch (Exception)
            {
                return Error("操作过于频繁");
            }
        }
        #endregion

        #region Post
        /// <summary>
        ///  修改关注者备注信息
        /// </summary>
        /// <param name="openid"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPut("{openid}/Actions/Remark")]
        [AllowAnonymous]
        public async Task<ResponseResult> UpdateRemark(string openid, [FromBody] MPUserActionsRemarkInput input)
        {
            var userModel = input.MapTo<MPUserModel>();
            userModel.openid = openid;
            await mPUserBll.UpdateRemark(userModel);
            return Success("修改成功");
        }

        /// <summary>
        /// 加入黑名单
        /// </summary>
        /// <param name="openId">关注者Id</param>
        /// <returns></returns>
        [HttpPost("{openId}/Actions/BlackList")]
        [AllowAnonymous]
        public async Task<ResponseResult> AddBatchBlack(string openId)
        {
            await mPUserBll.AddBatchBlack(openId);
            return Success("操作成功");
        }

        /// <summary>
        /// 移除黑名单
        /// </summary>
        /// <param name="openId">关注者Id</param>
        [HttpDelete("{openId}/Actions/BlackList")]
        [AllowAnonymous]
        public async Task<ResponseResult> DeleteBatchUnBlack(string openId)
        {
            await mPUserBll.DeleteBatchUnBlack(openId);
            return Success("操作成功");
        }
        #endregion
    }
}